//
//  HMNetworkTools.swift
//  Swift-网络请求类
//
//  Created by 范英强 on 16/1/28.
//  Copyright © 2016年 范英强. All rights reserved.
//

import UIKit
import AFNetworking
enum RequsetMethod:String {
    case GET = "GET"
    case POST = "POST"
}
class HMNetworkTools: AFHTTPSessionManager {
    
    static let sharedTools:HMNetworkTools = {
        let hmnetworkTool = HMNetworkTools()
        hmnetworkTool.responseSerializer.acceptableContentTypes?.insert("text/html")
        return hmnetworkTool
    }()
    

    func requset(method:RequsetMethod ,url: String ,params:AnyObject?,callback:(response:AnyObject?,error: NSError?)->()){
        
        //请求成功的闭包
        let succCallback = { (dataTask:NSURLSessionDataTask ,response: AnyObject?)-> Void in
            callback(response: response, error: nil)
        }
        
        let failureCallback = { (dataTask:NSURLSessionDataTask? ,error: NSError)-> Void in
            callback(response: nil, error: error)
        }

        
        if method == .GET {
            self.GET(url, parameters: params, progress: nil, success: succCallback, failure: failureCallback)
        } else{
            self.POST(url, parameters: params, progress: nil, success: succCallback, failure: failureCallback)
        }
    }

}
